package initialization

import (
	"embed"
	"github.com/gin-gonic/gin"
	"server-admin-api/api"
	"server-admin-api/middleware"
)

func InitRouter(engine *gin.Engine, fs embed.FS, flag bool) *gin.Engine {

	engine.Use(middleware.Cors())

	//resourceRouter(engine)

	group := engine.Group("v1")
	{
		groupRouter(group)     // 服务分组接口
		customizeRouter(group) // 自定义服务接口
		websocketRouter(group)
	}
	return engine

}

// resourceRouter 静态资源配置
func resourceRouter(engine *gin.Engine) {
	html := api.NewHtmlHandler()
	group := engine.Group("/ui")
	{
		group.GET("", html.Index)
	}
	// 解决刷新404问题
	engine.NoRoute(html.RedirectIndex)
}

func groupRouter(engine *gin.RouterGroup) {
	api := api.NewGroupApi()
	group := engine.Group("group")
	{
		group.GET("pages", api.Pages)
		group.GET("delete", api.Delete)
		group.POST("insert", api.Create)
	}
}


// customizeRouter 自定义服务相关
func customizeRouter(engine *gin.RouterGroup) {
	customize := api.NewCustomizeApi()
	group := engine.Group("customize")
	{
		group.POST("list", customize.ServerList)
		group.GET("running", customize.Run)
		group.POST("add", customize.Create)
		group.GET("delete", customize.Delete)
	}
}


func websocketRouter(engine *gin.RouterGroup) {
	ws := api.NewWebSocketApi()
	group := engine.Group("ws")
	{
		group.GET("", ws.RealTimeLog)  // 读取实时日志
	}
}
